#include "SequentialRandom.h"
#include <QTime>

SequentialRandom::SequentialRandom(int nMax, int nMin):mnMax(nMax),mnMin(nMin)
{
    QTime time(0,0,0);
    qsrand(time.secsTo(QTime::currentTime()));
    mnCount = mnMax - mnMin + 1;
}

int SequentialRandom::next()
{
    int nRnd = 0;

    if(mGenerated.count() == mnCount){
        mGenerated.clear();
    }

    if(mGenerated.count() == (mnCount - 1)){
        return dumpLowestNotGenerated();
    }

    int nLoopCount = 0;

    do{
        if(nLoopCount == 200){
            return dumpLowestNotGenerated();
        }

        nRnd = (qrand() % mnCount) + mnMin;
    }
    while(mGenerated.contains(nRnd));

    mGenerated.append(nRnd);

    return nRnd;
}

int SequentialRandom::dumpLowestNotGenerated()
{
    for(int i = mnMin; i <= mnMax; i++){
        if(!mGenerated.contains(i)){
            mGenerated.append(i);
            return i;
        }
    }

    return 0;
}
